<template>
  <div class="main">
    <div class="top-main">
      <dz-nav :is-back="true" :back-url="'/my'">
        <template #center>
          <b v-color><qq>Walletaddress</qq></b>
        </template>
      </dz-nav>
    </div>
    <div class="recharge">
      <div class="payments" v-if="!isDisabled">
        <div class="item" :class="{'active': curPaymentIndex === index}"
          v-for="(item, index) in payments" :key="index"
             @click="choosePayment(index)"
        >
          <img :src="item.img" alt="" class="icon">
          <p class="name">{{ item.name }}</p>
        </div>
      </div>
      <div class="info-input">
        <dz-input
            :disabled="isDisabled"
            v-model="formData.walletAddress"
            :rule="isRule"
            :rule-label="$t('PleaseenterWalletaddress')"
            type="text"
            :placeholder="$t('PleaseenterWalletaddress')"
            :label="$t('Walletaddress')"/>
      </div>
      <div class="bottom-box">
        <div class="btn" @click="handleAddWallet">
          <qq>confirm</qq>
        </div>
      </div>
    </div>
<!--    <div class="list">-->
<!--      <div class="lists">-->
<!--        <div class="item">-->
<!--          <p>-->
<!--            <span class="money">110 USDT</span>-->
<!--          </p>-->
<!--          <p>-->
<!--            <span class="desc">dcdvfdcdvfdvfvfbvfgbgdcdvfdcdvfdvfvfbvfgbg</span>-->
<!--          </p>-->
<!--        </div>-->
<!--      </div>-->
<!--    </div>-->
  </div>
</template>

<script>
import {
  walletAddressSelect,
  walletAddressList,
  setWalletAddress,
  getChannelTypeIsopen,
  getWalletAddress
} from '@/api/apiFox'
import {Toast} from "vant";
export default {
  data(){
    return{
      curPaymentIndex: 0,
      curPayment: {},
      payments: [],
      isDisabled:false,
      isRule:false,
      lists: [],
      formData: {
        walletAddress: ''
      }
    }
  },
  methods: {
    getPayments() {
      getWalletAddress().then(res=>{
        console.log(res.data)
        res.data ?  this.isDisabled=true : this.isDisabled=false

        this.formData.walletAddress = res.data
      }).catch(err=>{})
    },
    choosePayment(index) {
      this.curPaymentIndex = index
      this.curPayment = this.payments[this.curPaymentIndex]
    },
    handleAddWallet() {
      if (this.isDisabled){
        console.log('绑定+1')
      }else{
        console.log( this.curPayment)
        if (this.formData.walletAddress === ''){
          this.isRule=true
        }else{
          this.isRule=false
          setWalletAddress({
            channelType: this.curPayment.channelType,
            walletAddress: this.formData.walletAddress
          }).then(res=> {
            Toast.success({message:this.$t('Successfullyaddedwallet')})
            this.formData.walletAddress = ''
            this.curPaymentIndex = 0
            this.curPayment = this.payments[this.curPaymentIndex]
            this.getLists()
          }).catch(err => {})
        }
      }


    },
    getLists() {
      walletAddressList().then(res => {
        this.lists = res.data
      }).catch(err => {})
    },
    getPay(){
      getChannelTypeIsopen().then(res=>{
        let s = [
          {id:1,name:'USDT.TRC20',img:require('@/assets/images/TRC20.png'),channelType:res.trc20,isOpen:res.withdrawal_trc20},
          {id:2,name:'USDT.ERC20',img:require('@/assets/images/TRC20.png'),channelType:res.erc20,isOpen:res.withdrawal_erc20},
          {id:3,name:'USDT.Polygon',img:require('@/assets/images/Polygon.png'),channelType:res.polygon,isOpen:res.withdrawal_polygon},
          {id:4,name:'USDC.Polygon',img:require('@/assets/images/Polygon.png'),channelType:res.usdc_polygon,isOpen:res.withdrawal_usdc_polygon},
          {id:5,name:'ETH',img:require('@/assets/images/Property1=USDT.png'),channelType:res.default,isOpen:1},
          // {id:6,name:'balance payment',img:require('@/assets/images/bt.png'),channelType:res.default,isOpen:1},
        ]
        this.payments = s.filter(value => {return parseInt(value.isOpen) === 1})
        if (this.payments && this.payments.length>0){
          this.curPayment = this.payments[0]
        }
      }).catch(err=>{})
    }
  },

  mounted() {
    this.getPayments()
    this.getLists()
    this.getPay()
  }
}
</script>

<style scoped lang="scss">
.main{
  width: 100%;
  min-height: 100vh;
  background: url("@/assets/images/bg.jpg") top center no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
  .top-main{
    width: 100%;
    .avatar{
      padding-top: 10px;
      text-align: center;
      .img-avatar{
        width: 180px;
        height: 180px;
      }
    }
  }
  .recharge{
    padding: 15px 15px 0;
    .payments{
      display: flex;
      width: 100%;
      overflow: hidden;
      overflow-x: scroll;
      .item{
        width: 109px;
        height: 92px;
        background: #FFFFFF;
        box-shadow: 0px 4px 4px 0px rgba(160,58,58,0.25);
        border-radius: 70px 70px 70px 70px;
        padding: 16px 0;
        text-align: center;
        box-sizing: border-box;
        margin-right: 8px;
        flex-shrink: 0;
        &:last-child{
          margin-right: 0;
        }
        &.active{
          background: linear-gradient( 180deg, #8372FF 0%, #533DF1 100%);
          box-shadow: 0px 4px 4px 0px rgba(160,58,58,0.25);
          .name{
            color: #FFFFFF;
          }
        }
        .icon{
          width: 40px;
          height: 40px;
          //outline: 1px dashed red;
        }
        .name{
          font-weight: bold;
          font-size: 12px;
          color: #533DF1;
          //outline: 1px dashed red;
        }
      }
    }
    .info-input{
      margin-top: 20px;
    }
    .bottom-box{
      width: 100%;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
      .btn{
        width: 343px;
        height: 50px;
        background: linear-gradient( 90deg, #973DF1 0%, #5134C3 100%);
        box-shadow: 0px 4px 4px 0px rgba(92,18,187,0.25), inset 0px 4px 4px 0px rgba(249,249,249,0.25);
        border-radius: 30px 30px 30px 30px;
        font-weight: bold;
        font-size: 16px;
        color: #FFFFFF;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
  .list{
    height: 500px;
    background: linear-gradient( 180deg, #FFF0C9 0%, #D49188 100%);
    border-radius: 30px 30px 0px 0px;
    padding: 20px 15px;
    box-sizing: border-box;
    .lists{
      height: 460px;
      padding: 0 15px;
      background: #FFFFFF;
      border-radius: 20px;
      box-shadow: 0px 2px 4px 0px rgba(31,33,88,0.25);
      box-sizing: border-box;
      .item{
        padding: 10px 0;
        border-bottom: 1px solid rgba(133,133,138,0.2);
        p{
          display: flex;
          justify-content: space-between;
          font-size: 12px;
          color: #5B6B86;
          &:first-child{
            margin-bottom: 5px;
          }
          .desc{
            font-size: 14px;
            color: #242B35;
            word-break: break-all;
          }
          .money{
            font-weight: bold;
            font-size: 14px;
            color: #533DF1;
          }
        }
      }
    }
  }
}
</style>